package com.ruifu.proj.controller;

import com.fhzncloud.cloud.common.core.util.R;
import com.fhzncloud.cloud.common.core.util.StringUtils;
import com.fhzncloud.cloud.common.security.service.CloudUser;
import com.fhzncloud.cloud.common.security.util.SecurityUtils;
import com.ruifu.common.vo.PageResultVO;
import com.ruifu.common.vo.ResultVO;
import com.ruifu.proj.dto.ptmain.*;
import com.ruifu.proj.service.PmPtMainService;
import com.ruifu.proj.vo.page.ProjPage;
import com.ruifu.proj.vo.page.ProjPageResultVO;
import com.ruifu.proj.vo.pmtasklog.PmTaskLogVO;
import com.ruifu.proj.vo.ptmain.PmPtMainAndDocVO;
import com.ruifu.proj.vo.ptmain.PmPtMainTaskVO;
import com.ruifu.proj.vo.ptmain.PmPtMainTemplateQueryVO;
import com.ruifu.proj.vo.ptmain.PmPtMainVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/pmPtMain")
@Slf4j
public class PmPtMainController {
    @Autowired
    private PmPtMainService pmPtMainService;

    /**
     * 用于存储登陆人信息
     */
    private CloudUser cloudUser;

    /**
     * 通过url请求方法之前,调用该方法获取登陆人信息
     */
    @ModelAttribute
    public void before() {
        cloudUser = SecurityUtils.getUser();
    }


    /**
     * 根据项目/任务id 删除项目本身和子任务
     *
     * @param ptId
     * @return
     */
    @GetMapping(value = "/deleteById/{ptId}")
    public ResultVO deleteById(@PathVariable("ptId") Long ptId) {
        try {
            pmPtMainService.deleteById(ptId, cloudUser);
            return new ResultVO(true, "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultVO(false, "删除失败");
        }
    }

    /**
     * 根据id查询单个查询项目/任务
     *
     * @param ptId
     * @return
     */

    @GetMapping(value = "/findById/{ptId}")
    public PmPtMainVO findById(@PathVariable("ptId") Long ptId) {
        return pmPtMainService.findById(ptId);
    }

    /**
     * 查询项目/任务的子任务
     *
     * @param ptId
     * @return
     */
    @GetMapping(value = "/findByPtPId/{ptId}")
    public List<PmPtMainVO> findByPtPId(@PathVariable("ptId") Long ptId) {
        return pmPtMainService.findByPtPId(ptId);
    }


    /**
     * 分页查询项目/任务的子任务列表  2020.5.18
     *
     * @param ptId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping(value = "/findTasksPage/{ptId}/{pageNum}/{pageSize}")
    public PageResultVO findTasksPage(@PathVariable("ptId") Long ptId,
                                      @PathVariable("pageNum") int pageNum,
                                      @PathVariable("pageSize") int pageSize
    ) {
        pageNum = pageNum <= 0 ? 1 : pageNum;
        pageSize = pageSize < 5 ? 5 : pageSize;

        return pmPtMainService.findTasksPage(ptId, pageNum, pageSize, cloudUser);

    }


    /**
     * 查询项目/任务的【子子孙孙】任务列表  2020.5.19
     *
     * @param ptId
     * @return
     */
    @GetMapping(value = "/findAllTasks/{pageNum}/{pageSize}/{ptId}")
    public ProjPage findAllTasks(@PathVariable("pageNum") Integer pageNum,
                                 @PathVariable("pageSize") Integer pageSize,
                                 @PathVariable("ptId") Long ptId) {
        pageNum = pageNum <= 0 ? 1 : pageNum;
        pageSize = pageSize < 10 ? 10 : pageSize;
        ProjPage page=new ProjPage();
        page.setCurrentPage(pageNum);
        page.setPageSize(pageSize);

        return pmPtMainService.findAllTasks(page,ptId, cloudUser);
    }


    /**
     * 联合查询【项目管理】
     * 项目列表/任务列表
     *
     * @param pageNum
     * @param pageSize
     * @param pmPtMainQueryDTO
     * @return
     */
    @PostMapping(value = "/findByQuery/{pageNum}/{pageSize}")
    public PageResultVO findByQuery(
            @PathVariable("pageNum") int pageNum,
            @PathVariable("pageSize") int pageSize,
            @RequestBody PmPtMainQueryDTO pmPtMainQueryDTO) {
        pageNum = pageNum <= 0 ? 1 : pageNum;
        pageSize = pageSize < 5 ? 5 : pageSize;
        return pmPtMainService.findByQuery(pageNum, pageSize, pmPtMainQueryDTO, cloudUser);
    }

    /**
     * 新建项目、模板
     *
     * @param pmPtMainInsertDTO
     * @return
     */
    @PostMapping(value = "/insert")
    public ResultVO insert(@RequestBody PmPtMainInsertDTO pmPtMainInsertDTO) {
        try {
            pmPtMainService.insert(pmPtMainInsertDTO, cloudUser);
            return new ResultVO(true, "新建成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultVO(false, "新建失败");
        }
    }


    /**
     * 修改甘特图结构
     *
     * @param pmPtMainGTDTOList
     * @return
     */
    @PostMapping(value = "/updateList")
    public ResultVO updateList(@RequestParam(required = false) Long ptId, @RequestBody List<PmPtMainGTDTO> pmPtMainGTDTOList) {
        try {
            pmPtMainService.updateList(ptId, pmPtMainGTDTOList, cloudUser);
            return new ResultVO(true, "修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultVO(false, e.getMessage());
        }
    }

    /**
     * 根据项目、任务编号查询项目（保存新建项目code不重复）
     *
     * @param ptCode
     * @return
     */
    @GetMapping(value = "/findByPtCode/{ptCode}")
    public ResultVO findByPtCode(@PathVariable("ptCode") String ptCode) {
        if (pmPtMainService.findByPtCode(ptCode)) {
            return new ResultVO(true, "项目、任务编号查询不存在");
        } else {
            return new ResultVO(false, "项目、任务编号查询已存在");
        }
    }

    /**
     * 暂停、重启
     *
     * @param ptId    任务id
     * @param isPause 暂停标记
     * @return
     */
    @GetMapping(value = "/updateRunning/{ptId}/{isPause}")
    public ResultVO updateRunning(@PathVariable("ptId") Long ptId, @PathVariable("isPause") Boolean isPause) {
        try {
            pmPtMainService.updateRunning(ptId, isPause, cloudUser);
            return new ResultVO(true, "运行状态修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultVO(false, e.getMessage());
        }
    }

    /**
     * 一键重启
     *
     * @param ptId 任务id
     * @return
     */
    @GetMapping(value = "/updateRestart/{ptId}")
    public ResultVO updateRestart(@PathVariable("ptId") Long ptId) {
        try {
            pmPtMainService.updateRestart(ptId, cloudUser);
            return new ResultVO(true, "一键重启成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultVO(false, e.getMessage());
        }
    }

    /**
     * 下达
     *
     * @param ptId
     * @return
     */
    @GetMapping(value = "/issued/{ptId}")
    public ResultVO issued(@PathVariable("ptId") Long ptId) {
        try {
            pmPtMainService.issued(ptId, cloudUser);
            return new ResultVO(true, "下达成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultVO(false, e.getMessage());
        }
    }

    /**
     * 批量下达项目下的子任务
     *
     * @param ptId
     * @return
     */
    @GetMapping(value = "/batchIssued/{ptId}")
    public R batchIssued(@PathVariable("ptId") Long ptId) {
        return pmPtMainService.batchIssued(ptId, cloudUser);
    }

    /**
     * 启动
     *
     * @param ptId
     * @return
     */
    @GetMapping(value = "/start/{ptId}")
    public ResultVO start(@PathVariable("ptId") Long ptId) {
        try {
            pmPtMainService.start(ptId, cloudUser);
            return new ResultVO(true, "启动成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultVO(false, e.getMessage());
        }
    }

    /**
     * 提交
     *
     * @param ptId
     * @return
     */
    @GetMapping(value = "/submit/{ptId}")
    public ResultVO submit(@PathVariable("ptId") Long ptId) {
        try {
            pmPtMainService.submit(ptId, cloudUser);
            return new ResultVO(true, "提交成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultVO(false, e.getMessage());
        }
    }

    /**
     * 转交
     *
     * @param ptId       任务id
     * @param ptExecuter 负责人id(执行人)
     * @return
     */
    @GetMapping(value = "/careOf/{ptId}/{ptExecuter}")
    public ResultVO careOf(@PathVariable("ptId") Long ptId, @PathVariable("ptExecuter") Long ptExecuter) {
        try {
            pmPtMainService.careOf(ptId, ptExecuter, cloudUser);
            return new ResultVO(true, "转交成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultVO(false, e.getMessage());
        }
    }

    /**
     * 查询任务的日志记录
     *
     * @param ptId
     * @return
     */
    @GetMapping(value = "/findByLog/{ptId}")
    public List<PmTaskLogVO> findByLog(@PathVariable("ptId") Long ptId) {
        return pmPtMainService.findByLog(ptId);
    }

    /**
     * 审核
     *
     * @param pmPtMainAuditDTO
     * @return
     */
    @PostMapping(value = "/updateAudit")
    public ResultVO updateAudit(@RequestBody PmPtMainAuditDTO pmPtMainAuditDTO) {
        try {
            pmPtMainService.updateAudit(pmPtMainAuditDTO, cloudUser);
            return new ResultVO(true, "审批成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultVO(false, e.getMessage());
        }
    }

    /**
     * 撤销下达
     *
     * @param ptId 任务Id
     * @return
     */
    @GetMapping(value = "/revocationIssued/{ptId}")
    public ResultVO revocationIssued(@PathVariable Long ptId) {
        try {
            pmPtMainService.revocationIssued(ptId, cloudUser);
            return new ResultVO(true, "撤销下达成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultVO(false, e.getMessage());
        }
    }

    /**
     * 撤销转交
     *
     * @param ptId 任务Id
     * @return
     */
    @GetMapping(value = "/revocationDeliver/{ptId}")
    public ResultVO revocationDeliver(@PathVariable Long ptId) {
        try {
            pmPtMainService.revocationDeliver(ptId, cloudUser);
            return new ResultVO(true, "撤销转交成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultVO(false, e.getMessage());
        }
    }

    /**
     * 项目详情(含子任务)
     *
     * @param ptId
     * @return
     */
    @GetMapping(value = "/findAllByPtId/{ptId}")
    public List<PmPtMainVO> findAllByPtId(@PathVariable("ptId") Long ptId) {
        return pmPtMainService.findAllByPtId(ptId, cloudUser);
    }

    /**
     * 任务属性详情
     *
     * @param ptId
     * @return
     */
    @GetMapping(value = "/findByIdTask/{ptId}")
    public PmPtMainAndDocVO findByTask(@PathVariable("ptId") Long ptId) {
        return pmPtMainService.findByTask(ptId, cloudUser);
    }

    /**
     * 更换执行人/指定执行人
     *
     * @param ptExecuter
     * @param ptId
     * @return
     */
    @GetMapping(value = "/updateByExecuter/{ptId}/{ptExecuter}")
    public ResultVO updateByExecuter(@PathVariable("ptId") Long ptId, @PathVariable("ptExecuter") Long ptExecuter) {
        try {
            pmPtMainService.updateByExecuter(ptId, ptExecuter, cloudUser);
            return new ResultVO(true, "更换/指定成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultVO(false, "更换/指定失败");
        }
    }

    /**
     * 查询项目、任务的计划页
     *
     * @param ptId
     * @return
     */
    @GetMapping(value = "/findPlanById/{pageNum}/{pageSize}/{ptId}")
    public ProjPageResultVO findPlanById(@PathVariable(value = "pageNum") Integer pageNum,
                                         @PathVariable(value = "pageSize") Integer pageSize,
                                         @PathVariable("ptId") Long ptId) {
        pageNum = pageNum <= 0 ? 1 : pageNum;
        pageSize = pageSize < 10 ? 10 : pageSize;

        return pmPtMainService.findPlanById(pageNum,pageSize,ptId, cloudUser);
    }

    /**
     * 新增计划版本
     *
     * @param pmPtMainPlanDTO
     * @return
     */
    @PostMapping(value = "/savePlan")
    public ResultVO savePlan(@RequestBody PmPtMainPlanDTO pmPtMainPlanDTO) {
        try {
            pmPtMainService.savePlan(pmPtMainPlanDTO, cloudUser);
            return new ResultVO(true, "保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultVO(false, e.getMessage());
        }
    }

    /**
     * 对编辑中的计划变更进行修改
     * @param pmPlanVerDTO
     * @return
     */
    @PostMapping(value = "/editPlan")
    public ResultVO editPlan(@RequestBody PmPlanVerDTO pmPlanVerDTO) {
        try {
            pmPtMainService.editPlan(pmPlanVerDTO, cloudUser);
            return new ResultVO(true, "更新成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultVO(false, e.getMessage());
        }
    }



    /**
     * 提交计划变更
     * @param pvId
     * @return
     */
    @GetMapping(value = "/submitPlan/{pvId}")
    public ResultVO submitPlan(@PathVariable(value = "pvId")Long pvId) {
        try {
            pmPtMainService.submitPlan(pvId);
            return new ResultVO(true, "提交成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultVO(false, e.getMessage());
        }
    }


    /**
     * 驳回计划变更  恢复状态为编辑中
     * @param pvId
     * @return
     */
    @GetMapping(value = "/rejectPlan/{pvId}")
    public ResultVO rejectPlan(@PathVariable(value = "pvId")Long pvId) {
        try {
            pmPtMainService.rejectPlan(pvId);
            return new ResultVO(true, "驳回成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultVO(false, e.getMessage());
        }
    }



    /**
     * 保存任务属性
     *
     * @param pmPtMainAttributeDTO
     * @return
     */
    @PostMapping(value = "/saveAttributeById")
    public ResultVO saveAttributeById(@RequestBody PmPtMainAttributeDTO pmPtMainAttributeDTO) {
        try {
            pmPtMainService.saveAttributeById(pmPtMainAttributeDTO, cloudUser);
            return new ResultVO(true, "保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultVO(false, "保存失败");
        }
    }

    /**
     * 查询项目阶段配置可选任务列表
     *
     * @param ptId
     * @return
     */
    @GetMapping(value = "/findTaskByPtId")
    public List<PmPtMainTaskVO> findTaskByPtId(Long ptId) {
        return pmPtMainService.findTaskByPtId(ptId);
    }

    /**
     * 查询可选的启停条件任务列表
     *
     * @param pmPtId 当前任务id
     * @return
     */
    @GetMapping(value = "/findCondTask/{pmPtId}")
    public List<PmPtMainTaskVO> findCondTask(@PathVariable Long pmPtId) {
        return pmPtMainService.findCondTask(pmPtId);
    }

    /**
     * 查询模板页
     *
     * @param pageNum                  当前页码
     * @param pageSize                 每页大小
     * @param pmPtMainTemplateQueryDTO 查询对象
     * @return
     */
    @PostMapping(value = "/findTemplateByQuery/{pageNum}/{pageSize}")
    public PageResultVO findTemplateByQuery(@PathVariable("pageNum") int pageNum,
                                            @PathVariable("pageSize") int pageSize,
                                            @RequestBody PmPtMainTemplateQueryDTO pmPtMainTemplateQueryDTO) {
        return pmPtMainService.findTemplateByQuery(pageNum, pageSize, pmPtMainTemplateQueryDTO, cloudUser);
    }


    /**
     * 批量删除
     *
     * @param ptIds 模板id数组
     * @return
     */
    @PostMapping(value = "/deletePL")
    public ResultVO deletePL(@RequestBody Long[] ptIds) {
        try {
            pmPtMainService.deletePL(ptIds, cloudUser);
            return new ResultVO(true, "批量删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultVO(false, "批量删除失败");
        }
    }

    /**
     * 修改模板的状态
     *
     * @param ptId     模板id
     * @param ptStatus 模板要修改的状态 TA -> TB <-> TP
     * @return
     */
    @GetMapping("/updateTemplateStatus/{ptId}/{ptStatus}")
    public ResultVO updateTemplateStatus(@PathVariable Long ptId, @PathVariable String ptStatus) {
        try {
            pmPtMainService.updateTemplateStatus(ptId, ptStatus, cloudUser);
            return new ResultVO(true, "修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultVO(false, "修改失败");
        }
    }

    /**
     * 使用模板
     *
     * @param ptId 模板id
     * @return
     */
    @GetMapping("/useTemplate/{ptId}")
    public ResultVO useTemplate(@PathVariable Long ptId) {
        try {
            pmPtMainService.useTemplate(ptId, cloudUser);
            return new ResultVO(true, "使用模板成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultVO(false, "使用模板失败");
        }
    }

    /**
     * 我的项目任务
     * 【我委托、负责的项目】
     *
     * @param pageNum
     * @param pageSize
     * @param pmPtMainPanelDTO
     * @return
     */
    @PostMapping(value = "/findPanelByQuery/{pageNum}/{pageSize}")
    public PageResultVO findPanelByQuery(@PathVariable("pageNum") int pageNum,
                                         @PathVariable("pageSize") int pageSize,
                                         @RequestBody PmPtMainPanelDTO pmPtMainPanelDTO) {
        pageNum = pageNum <= 0 ? 1 : pageNum;
        pageSize = pageSize < 5 ? 5 : pageSize;
        return pmPtMainService.findPanelByQuery(pageNum, pageSize, pmPtMainPanelDTO, cloudUser);
    }

    /**
     * 个人面板-选择父任务列表
     *
     * @param pageNum  当前页码
     * @param pageSize 每页大小
     * @param ptName   任务名称
     * @return
     */
    @GetMapping(value = "/findParentByPtName/{pageNum}/{pageSize}")
    public PageResultVO findParentByPtName(@PathVariable("pageNum") int pageNum,
                                           @PathVariable("pageSize") int pageSize,
                                           @RequestParam(required = false) String ptName) {
        pageNum = pageNum <= 0 ? 1 : pageNum;
        pageSize = pageSize < 5 ? 5 : pageSize;
        return pmPtMainService.findParentByPtName(pageNum, pageSize, ptName, cloudUser);
    }

    /**
     * 新建任务-个人面板
     *
     * @param pmPtMainInsertTaskDTO
     */
    @PostMapping(value = "/insertTask")
    public ResultVO insertTask(@RequestBody PmPtMainInsertTaskDTO pmPtMainInsertTaskDTO) {
        try {
            pmPtMainService.insertTask(pmPtMainInsertTaskDTO, cloudUser);
            return new ResultVO(true, "新建任务成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultVO(false, e.getMessage());
        }
    }

    /**
     * 查询所有模板列表
     *
     * @param ptName 模板名称
     * @return
     */
    @GetMapping(value = "/findTemplateListByPtName")
    public List<PmPtMainTemplateQueryVO> findTemplateListByPtName(@RequestParam(required = false) String ptName) {
        return pmPtMainService.findTemplateListByPtName(ptName, cloudUser);
    }

    /**
     * 根据用户id和任务id查询权限
     *
     * @param ptId
     * @return
     */
    @GetMapping(value = "/findPermissionByUserIdAndPtId/{ptId}")
    public Integer findPermissionByUserIdAndPtId(@PathVariable("ptId") Long ptId) {
        return pmPtMainService.findPermissionByUserIdAndPtId(Long.valueOf(cloudUser.getId()), ptId);
    }

    /**
     * 获取项目/任务编号
     *
     * @param ptPtm 查询类别
     * @return
     */
    @GetMapping(value = "/getProjectCode/{ptPtm}")
    public String getProjectCode(@PathVariable String ptPtm) {
        return pmPtMainService.getProjectCode(ptPtm);
    }

    /**
     * 获取模板项目编号
     *
     * @return
     */
    @GetMapping(value = "/getTemplateCode")
    public String getTemplateCode() {
        return pmPtMainService.getTemplateCode();
    }

    @GetMapping(value = "/test/{ptId}")
    public R test(@PathVariable Long ptId) {
        return pmPtMainService.test(ptId, cloudUser);
    }

    /***
     * 降级/升级
     */
    @GetMapping("/changeLevel/{ptId}/{state}")
    public R downLevel(@PathVariable("ptId") Long ptId, @PathVariable("state") String state) {
        try {
            String code = pmPtMainService.changeLevel(ptId, state, cloudUser);
            return R.ok(code);
        } catch (Exception e) {
            e.printStackTrace();
            return R.failed(false, e.getMessage());
        }

    }

    /**
     * 一键保存   【项目详情】   2020.5.18
     */
    @PostMapping(value = "/saveAll")
    public ResultVO saveAll(@RequestBody SavePmPtMainAttributeDTO savePmPtMainAttributeDTO) {
        try {
            pmPtMainService.saveAll(savePmPtMainAttributeDTO, cloudUser);
            return new ResultVO(true, "保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultVO(false, e.getMessage());
        }
    }


    /**
     * 一键新建  【新建项目】    2020.5.18
     *
     * @param createNewPmPtMainDTO
     * @return
     */
    @PostMapping(value = "/insertAll")
    public ResultVO insertAll(@RequestBody CreateNewPmPtMainDTO createNewPmPtMainDTO) {
        try {
            pmPtMainService.insertAll(createNewPmPtMainDTO, cloudUser);
            return new ResultVO(true, "新建成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultVO(false, e.getMessage());
        }
    }

    /***
     * 2020.5.19 给王荣华  查看审核结果接口
     * @param ptId
     * @return
     */
    @GetMapping(value = "/findTaskAudit/{ptId}/{paId}")
    public PmTaskAuditDTO findTaskAudit(@PathVariable("ptId") Long ptId,
                                        @PathVariable("paId") Long paId) {


        return pmPtMainService.findTaskAuditByPtId(ptId, paId, cloudUser);

    }


    /**
     * 2020.5.19  给董烨的接口
     *
     * @param sourceId
     * @param targetId
     * @param type
     * @return
     */
    //@Inner
    @GetMapping(value = "/exchange/{sourceId}/{targetId}/{type}")
    public Boolean exchange(@PathVariable("sourceId") Integer sourceId,
                            @PathVariable("targetId") Integer targetId,
                            @PathVariable("type") String type) {
        if (null == sourceId || null == targetId || StringUtils.isBlank(type)) {
            log.error("参数异常");
            return false;
        }
        type = type.toUpperCase();
        try {
            pmPtMainService.exchange(sourceId, targetId, type, cloudUser);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }


}
